package com.ruoyi.web.controller.mes;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.domain.MesInventoryDetail;
import com.ruoyi.web.domain.mes.MesPaper;
import com.ruoyi.web.domain.vo.InventoryVo;
import com.ruoyi.web.domain.vo.MesInventoryInfoVo;
import com.ruoyi.web.domain.vo.StatVo;
import com.ruoyi.web.service.Impl.MesPaperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.domain.MesInventoryInfo;
import com.ruoyi.web.service.IMesInventoryInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 盘点Controller
 *
 * @author wxt
 * @date 2024-01-24
 */
@Api("纸张盘点管理")
@RestController
@RequestMapping("/mes/inventory/info")
public class MesInventoryInfoController extends BaseController {
    @Autowired
    private IMesInventoryInfoService mesInventoryInfoService;
    @Resource
    private MesPaperService mesPaperService;
    /**
     * 查询盘点列表
     */
    @ApiOperation("查询盘点列表")
    @GetMapping("/list")
    public TableDataInfo list(MesInventoryInfo mesInventoryInfo) {
        startPage();
        List<MesInventoryInfo> list = mesInventoryInfoService.selectMesInventoryInfoList(mesInventoryInfo);
        return getDataTable(list);
    }

    /**
     * 查询盘点明细
     */
    @ApiOperation("查询盘点明细")
    @GetMapping("/inventoryDetailList")
    public TableDataInfo inventoryDetailList(MesInventoryInfo mesInventoryInfo) {

        startPage();
        List<MesInventoryInfoVo> list = mesInventoryInfoService.selectMesInventoryDetailList(mesInventoryInfo);
        //profitLossSum
        return getDataTable(list);
    }

    /**
     * 导出盘点列表
     */
    @ApiOperation("导出盘点列表")
    @Log(title = "盘点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesInventoryInfo mesInventoryInfo) {
        List<MesInventoryInfo> list = mesInventoryInfoService.selectMesInventoryInfoList(mesInventoryInfo);
        ExcelUtil<MesInventoryInfo> util = new ExcelUtil<MesInventoryInfo>(MesInventoryInfo.class);
        util.exportExcel(response, list, "盘点数据");
    }

    /**
     * 获取盘点详细信息
     */
    @ApiOperation("获取盘点详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(mesInventoryInfoService.selectMesInventoryInfoById(id));
    }


    /**
     * 盘点明细查询
     */

    @ApiOperation("盘点明细查询")
    @PostMapping("/Detail")
    public TableDataInfo Detail(@RequestBody Map<String, Object> params) {
//        String type = (String) params.get("type");//仓库或机台
        String machineId = (String) params.get("machineId");//仓库或机台
//        startPage();
        MesInventoryInfo Inventory = new MesInventoryInfo();
        Inventory.setParams(params);
        if (machineId!=null && machineId!="0") {//机台
            Date date = mesInventoryInfoService.selectRecently(machineId);//机台最后一次盘点时间
            params.put("inventoryDate", date);
            Inventory.setParams(params);
            List<InventoryVo> list = mesInventoryInfoService.selectDayInventory(Inventory);
            return getDataTable(list);
        } else {//仓库
            List<InventoryVo> list = mesInventoryInfoService.selectMesDetaillist(Inventory);
            return getDataTable(list);
        }
    }


    @ApiOperation("统计查询")
    @GetMapping("/stat") //@PostMapping
    public TableDataInfo stat(MesInventoryInfo inventory) throws ParseException //@RequestBody  Map<String,Object> params  //ParamsVo params
    {
        String type = inventory.getMachineId();
        Map<String, Object> params = new HashMap<String, Object>();
        //params.put("unit", inventory.);
        params.put("brand", inventory.getBrand());
        params.put("name", inventory.getName());
        params.put("code", inventory.getCode());
        params.put("specs", inventory.getSpecs());
        params.put("material", inventory.getMaterial());
        params.put("weight", inventory.getWeight());
        params.put("longWide", inventory.getWeight());
        params.put("pageNum", inventory.getPageNum());
        params.put("pageSize", inventory.getPageSize());
        params.put("packageType", inventory.getPackageType());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        if (inventory.getInventoryDate() != null) {
//            params.put("inventoryDate", sdf.format(inventory.getInventoryDate()));
//        }
        if(StringUtils.isNotEmpty(inventory.getSerachDate())){
            params.put("inventoryDate",sdf.parse(inventory.getSerachDate()));
            inventory.setInventoryDate(sdf.parse(inventory.getSerachDate()));
        }
        params.put("machineId", inventory.getMachineId());
        inventory.setParams(params);

        if (StringUtils.isNotBlank(type)) {
            if (type.equals("0")) {//仓库
                List<StatVo> list = mesInventoryInfoService.warehousestat(inventory);
                return getDataTable(list);
            } else if (type.equals("1")) {//白纸裁切组
                List<StatVo> list = mesInventoryInfoService.cuttingGroupsStat(inventory);
                return getDataTable(list);
            } else {//机台
                List<StatVo> list = mesInventoryInfoService.machinestat(inventory);
                return getDataTable(list);
            }
        } else if (StringUtils.isNotBlank(inventory.getCode()) || StringUtils.isNotBlank(inventory.getName())) {
           // List<StatVo> list = mesInventoryInfoService.warehousestat(inventory);
            List<StatVo> list = mesInventoryInfoService.machinestat(inventory);
            return getDataTable(list);
        }
        return null;
    }


    /**
     * 导出盘点列表
     */
    @ApiOperation("导出纸张库存统计")
    @Log(title = "纸张库存统计", businessType = BusinessType.EXPORT)
    @PostMapping("/paperInventoryExport")
    public void paperInventoryExport(HttpServletResponse response,@RequestBody MesInventoryInfo inventory) throws ParseException {
        String type = inventory.getMachineId();
        Map<String, Object> params = new HashMap<String, Object>();
        //params.put("unit", inventory.);
        params.put("brand", inventory.getBrand());
        params.put("name", inventory.getName());
        params.put("code", inventory.getCode());
        params.put("specs", inventory.getSpecs());
        params.put("material", inventory.getMaterial());
        params.put("weight", inventory.getWeight());
        params.put("longWide", inventory.getWeight());
        params.put("pageNum", inventory.getPageNum());
        inventory.setPageSize(1000);
        params.put("pageSize", inventory.getPageSize());
        params.put("packageType", inventory.getPackageType());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        if (inventory.getInventoryDate() != null) {
//            params.put("inventoryDate", sdf.format(inventory.getInventoryDate()));
//        }
        if(StringUtils.isNotEmpty(inventory.getSerachDate())){
            params.put("inventoryDate",sdf.parse(inventory.getSerachDate()));
            inventory.setInventoryDate(sdf.parse(inventory.getSerachDate()));
        }


        params.put("machineId", inventory.getMachineId());
        inventory.setParams(params);
        List<StatVo> list=new ArrayList<>();
        if (StringUtils.isNotBlank(type)) {
            if (type.equals("0")) {//仓库
              list = mesInventoryInfoService.warehousestat(inventory);
            }else  if (type.equals("1")) {//白纸裁切组
               list = mesInventoryInfoService.cuttingGroupsStat(inventory);
            } else {//机台
              list = mesInventoryInfoService.machinestat(inventory);
            }
        } else if (StringUtils.isNotBlank(inventory.getCode()) || StringUtils.isNotBlank(inventory.getName())) {
              list = mesInventoryInfoService.machinestat(inventory);
        }
        ExcelUtil<StatVo> util = new ExcelUtil<StatVo>(StatVo.class);
        util.exportExcel(response, list, "纸张库存统计");
    }

    /**
     * 新增盘点
     */
    @ApiOperation("新增盘点")
    @Log(title = "盘点", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesInventoryInfo mesInventoryInfo) {
        return toAjax(mesInventoryInfoService.insertMesInventoryInfo(mesInventoryInfo));
    }

    /**
     * 修改盘点
     */
    @ApiOperation("修改盘点")
    @Log(title = "盘点", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesInventoryInfo mesInventoryInfo) {
        return toAjax(mesInventoryInfoService.updateMesInventoryInfo(mesInventoryInfo));
    }

    /**
     * 删除盘点
     */
    @ApiOperation("删除盘点")
    @Log(title = "盘点", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mesInventoryInfoService.deleteMesInventoryInfoByIds(ids));
    }
}
